package com.tjsoft.system.web.account;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.tjsoft.system.entity.base.Comp;
import com.tjsoft.system.entity.base.Moneyitem;
import com.tjsoft.system.entity.base.Selfmoney;
import com.tjsoft.system.service.AccountManager;
import com.tjsoft.system.web.CrudActionSupport;

import edu.emory.mathcs.backport.java.util.Collections;

//namespace与application下的目录对应dispatcher
@Namespace("/app/sys/selfmoney")
@Results( { @Result(name = "reload", location = "/app/sys/selfmoney/selfmoney!list", type = "redirect")})
public class SelfmoneyAction extends CrudActionSupport<Selfmoney> {

	private static final long serialVersionUID = 8683878162525847072L;

	@Autowired
	private AccountManager accountManager;

	private Long id ;
	private Selfmoney entity;
	private Page<Selfmoney> page = new Page<Selfmoney>(10);
	private Long[] chkDel; 
	private String errMsg = "";
	private List<Comp> comps = new ArrayList<Comp>();
	private List<Moneyitem> items = new ArrayList<Moneyitem>();
	
	public void validateSave(){
		try {
			if(entity.getComp().getId()==null || "".equals(entity.getComp().getId())){
				this.addFieldError("msg.compName", "请选择企业名称");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	@Override
	public String delete()  {
		try {
			if(chkDel!=null && chkDel.length>0){
				for(Long id:chkDel){
					accountManager.deleteSelfmoney(id);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除微型企业自有资金使用记录失败");
		}
		return RELOAD;
	}
	@Override
	public String input()  {
		return INPUT;
	}
	
	@Override
	public String list() {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
			page = accountManager.getSelfDAO().findPage(page, filters);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("加载微型企业自有资金使用记录失败");
		}
		return "list";
	}
	@Override
	protected void prepareModel()  {
		if (id != null) {
			try {
				entity = accountManager.getSelfDAO().get(id);
				Collections.sort(entity.getItems());
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
				addActionMessage("加载微型企业自有资金使用记录失败");
			}
		} else {
			entity = new Selfmoney();
		}
		comps = accountManager.getCompDAO().getAll();
	}
	@Override
	public String save()  {
		try {
			entity.setItems(items);
			double totalMoney = 0.0d;
			for(Moneyitem item: items){
				totalMoney += item.getAmount();
			}
			entity.setTotalMoney(totalMoney);
			accountManager.saveSelfmoney(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionMessage("保存微型企业自有资金使用记录失败");
		}
		return RELOAD;
	}
	
	public Selfmoney getModel() {
		return entity;
	}
	
	//＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
	public AccountManager getAccountManager() {
		return accountManager;
	}
	
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	public Page<Selfmoney> getPage() {
		return page;
	}
	public void setPage(Page<Selfmoney> page) {
		this.page = page;
	}
	public Selfmoney getEntity() {
		return entity;
	}
	public void setEntity(Selfmoney entity) {
		this.entity = entity;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public Long[] getChkDel() {
		return chkDel;
	}

	public void setChkDel(Long[] chkDel) {
		this.chkDel = chkDel;
	}

	public List<Comp> getComps() {
		return comps;
	}

	public void setComps(List<Comp> comps) {
		this.comps = comps;
	}

	public List<Moneyitem> getItems() {
		return items;
	}

	public void setItems(List<Moneyitem> items) {
		this.items = items;
	}
	
	public int getItemCount(){
		return entity.getItems().size();
	}
}
